package org.jslaughter.component.pickup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jslaughter.event.eventtype.DeathEvent;
import org.jslaughter.event.eventtype.DeathListener;
import org.nvframe.component.AbstractComponent;
import org.nvframe.component.state.Position;
import org.nvframe.entity.Entity;
import org.nvframe.exception.NVFrameException;
import org.nvframe.factory.EntityFactory;
import org.nvframe.util.settings.SettingsObj;

public class DropPickup extends AbstractComponent implements DeathListener {
	
	private List<String> pickups = new ArrayList<String> ();

	public DropPickup(String id, Entity owner, SettingsObj settings) {
		super(id, owner);
		
		pickups = settings.getList("pickups") != null ? 
				settings.getList("pickups").getStrings() : 
					new ArrayList<String> (Arrays.asList(settings.getString("pickups")));
	}

	@Override
	public void onDeath(DeathEvent event) {
		if(event.getEntity() == owner) {
			Position position = (Position) owner.getComponent(Position.class);
			
			for(String entityId : pickups) {
				try {
					Entity entPickup = EntityFactory.getInstance().getEntityFromPrototype(entityId);
					Position corpsePosition = (Position) entPickup.getComponent(Position.class);
					
					corpsePosition.setXY(position.getXY());
				} catch (NVFrameException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
